<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\FeishuWebAuth;
use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseOrderProcessModel;
use App\Service\BaseService;
use App\Service\Cruise\Check\CruiseCheckAgainstService;
use App\Service\Cruise\Mission\CheckInputService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class CruiseCheckAgainstController extends BaseController
{

    /**
     * 开具违约函
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ValidationException|GuzzleException
     */
    public function drawUpAgainst(Request $request, int $id): JsonResponse
    {
        // todo
//        $this->validate([
//            'auditContent' => 'required|string'
//        ]);

        //是否开具违约函   0 不开具   1  开具
        $whether = (int)$request->post('whether', 0);
        if ($whether == 1) {
            $this->validate(
                $request, [
                    'level'        => 'required',
                    'type'         => 'required',
                    'behaviorDesc' => 'required',
                    'rectifyPlan'  => 'required',
                ]
            );
            CruiseCheckAgainstService::addAgainst($id, $request->level, implode(',', $request->type), (int)$request->price, $request->behaviorDesc, $request->rectifyPlan);
        }
        $veto = $request->post('veto', CruiseCheckDetailModel::VETO_ON);
        // 审核通过
        $data = [
            'auditStatus'  => CruiseOrderProcessModel::STATUS_OK,
            'auditContent' => (string)$request->post('auditContent'),
            'veto'         => $veto,
        ];
        (new CheckInputService($id, FeishuWebAuth::$userId))->audit($data, 1, $whether);

        return $this->success();
    }

    /**
     * 违约函列表
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function lists(Request $request): JsonResponse
    {
        $startTime    = (string)$request->get('startTime', '');
        $endTime      = (string)$request->get('endTime', '');
        $level        = (int)$request->get('level', '');
        $name         = (string)$request->get('name', '');
        $pageSize     = (int)$request->get('pageSize', 20);
        $pageNum      = (int)$request->get('pageNum', 1);
        $status       = (string)$request->get('status', '');
        $startPayTime = (string)$request->get('startPayTime', '');
        $endPayTime   = (string)$request->get('endPayTime', '');

        return $this->success(
            CruiseCheckAgainstService::lists(
                $startTime, $endTime, $level, $name, $pageSize, $pageNum, $status, $startPayTime, $endPayTime
            )
        );
    }


    /**
     * 违约函详情
    督导整改违约 - 确认
    $data = [
    'auditStatus'  => CruiseOrderProcessModel::STATUS_OK,
    'auditContent' => $auditContent, // 整改措施
    ];

    (new CheckInputService($cruiseOrderId, $userId))->audit($data);
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function detail(Request $request, int $id): JsonResponse
    {
        return $this->success(CruiseCheckAgainstService::detail($id));
    }

    /**
     * 新增违约函
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function add(Request $request): JsonResponse
    {

        $this->validate(
            $request, [
                'shopId'       => 'required|integer',
                'type'         => "required|string",
                'level'        => 'required|integer|between:1,2',
                'behaviorDesc' => 'required|string|min:0|max:255',
                'rectifyPlan'  => 'required|string|min:0|max:255',
                'startTime'    => "required|date",
            ]
        );
        $price = (int)$request->get('price', 0);
        $type  = (string)$request->get('type', '');
        $type  = explode(',', $type);
        if (!$type || !is_array($type)) {
            return BaseService::throwErr("type不能为空");
        }
        if ($request->level == CruiseCheckAgainstModel::LEVEL_PUNISH && $price == 0) {//处罚才有金额
            return BaseService::throwErr("金额不能为0");
        }

        CruiseCheckAgainstService::add($request, $price);

        return $this->success();
    }

    /**
     * 处理修改违约函
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ValidationException
     */
    public function edit(Request $request, int $id): JsonResponse
    {
        $request = $request->merge(['id' => $id]);

        $this->validate(
            $request, [
                'id'    => 'required|integer',
                'level' => 'required|integer|between:1,2',
            ]
        );
        $price = (int)$request->get('price', 0);
        if ($request->level == CruiseCheckAgainstModel::LEVEL_PUNISH && $price == 0) {//处罚才有金额
            return BaseService::throwErr("金额不能为0");
        }
        CruiseCheckAgainstService::edit($id, $request);

        return $this->success();
    }

    /**
     * 撤销违约函
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ValidationException
     */
    public function cancel(Request $request, int $id): JsonResponse
    {
        $request = $request->merge(['id' => $id]);

        $this->validate(
            $request, [
                'id'     => 'required|integer',
                'reason' => 'string',
            ]
        );
        CruiseCheckAgainstService::cancel($request);

        return $this->success();
    }

    /**
     * 查询门店违约函对应版本
     * @param Request $request
     * @param int $shopId
     * @return JsonResponse
     * @throws ValidationException|GuzzleException
     */
    public function shopVersion(Request $request, int $shopId): JsonResponse
    {
        $request = $request->merge(['shopId' => $shopId]);

        $this->validate(
            $request, [
                'shopId'    => 'required|integer|min:0',
            ]
        );
        return $this->success(CruiseCheckAgainstService::shopVersion($shopId));
    }

    /**
     * 门店违约函版本预览返回数据
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function shopVersionPreview(Request $request): JsonResponse
    {
        $shopId  = (int)$request->get('shopId', 0);
        $id      = (int)$request->get('id', 0);
        $checkId = (int)$request->get('checkId', 0);
        if ($shopId == 0 && $id == 0) {
            return BaseService::throwErr("参数错误");
        }
        return $this->success(CruiseCheckAgainstService::shopVersionPreview($shopId, $id, 0, $checkId));
    }
}
